package com.springboot.demo.service.impl;


import com.cx.item.common.exception.CommonException;
import com.cx.item.common.par.PageParam;
import com.cx.item.common.service.impl.BaseServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.springboot.demo.mapper.GoodsMapper;
import com.springboot.demo.mapper.GoodsTypeMapper;
import com.springboot.demo.mapper.OrderMapper;
import com.springboot.demo.model.example.OrderExample;
import com.springboot.demo.model.model.Order;
import com.springboot.demo.model.par.OrderParForStatus;
import com.springboot.demo.model.vo.OrderVo;
import com.springboot.demo.service.OrderBGService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by heavin on 2018/6/21.
 */
@Service
public class OrderBGServiceImpl extends BaseServiceImpl<Order, OrderExample, OrderMapper> implements OrderBGService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsTypeMapper goodsTypeMapper;


    @Override
    public PageInfo<List<OrderVo>> queryList(PageParam pageParam) {
        OrderExample example = new OrderExample();
        OrderExample.Criteria criteria = example.createCriteria();
        criteria.andOrderStatusEqualTo(1);
        example.setOrderByClause("create_time desc");

        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        List<Order> orderList = orderMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo(orderList);

        List<OrderVo> orderVoList = new ArrayList<>();
        OrderVo orderVo;
        for (Order order : orderList) {
            orderVo = new OrderVo();
            BeanUtils.copyProperties(order, orderVo);
            orderVoList.add(orderVo);
        }

        pageInfo.setList(orderVoList);
        return pageInfo;
    }

    @Override
    public int updateStatus(OrderParForStatus orderParForStatus) {
        Order order = new Order();
        BeanUtils.copyProperties(orderParForStatus, order);

        order.setUpdateTime(new Date());
        return orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public int delById(Integer id) {
        Order order = new Order();
        order.setOrderId(id);
        order.setOrderStatus(0);
        return orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public OrderVo queryById(Integer id) {
        Order order = orderMapper.selectByPrimaryKey(id);
        if (null == order) {
            throw new CommonException("您查询的订单不存在");
        }
        OrderVo orderVo = new OrderVo();
        BeanUtils.copyProperties(order, orderVo);
        return orderVo;
    }
}
